package 简单.模拟;

/**
 * 给你两个字符串 word1 和 word2 。请你从 word1 开始，通过交替添加字
 * 母来合并字符串。如果一个字符串比另一个字符串长，就将多出来的字母追
 * 加到合并后字符串的末尾。
 * 返回 合并后的字符串 。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode.cn/problems/merge-strings-alternately
 */
public class 交替合并字符串_1768 {

    public static void main(String[] args) {

    }

    public String mergeAlternately(String word1, String word2) {
        StringBuilder stringBuilder = new StringBuilder();

        if (word1.length() >= word2.length()) {
            for (int i = 0; i < word2.length(); i++) {
                stringBuilder.append(word1.charAt(i))
                        .append(word2.charAt(i));
            }
            stringBuilder.append(word1.substring(word2.length()));
        }

        if (word1.length() < word2.length()) {
            for (int i = 0; i < word1.length(); i++) {
                stringBuilder.append(word1.charAt(i))
                        .append(word2.charAt(i));
            }
            stringBuilder.append(word2.substring(word1.length()));
        }

        return stringBuilder.toString();
    }

}
